package com.shanling.shanlingcontroller.receiver;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.services.csr.GAIABREDRProvider;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;

import java.util.List;

import butterknife.internal.Utils;

/**
 * Created by ch1 on 2019/11/27.
 */

public class A2DPConnectionStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
            PreferenceUtil instance = PreferenceUtil.getInstance(CustomApplication.getInstance());
            int state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 0);
            switch (state) {
                case 0:
                    //disconnected
                    Log.e("weiyu","a2dp连接失败");
                    instance.setA2dpstate(false);
                    EventBus.getDefault().post(new EventCenter<>(Constants.A2DP_ISCONNTECT, false));
                    break;
                case 2:
                    Log.e("weiyu","a2dp连接成功");
                    instance.setA2dpstate(true);
                    EventBus.getDefault().post(new EventCenter<>(Constants.A2DP_ISCONNTECT, true));
                    break;
                default:
                    instance.setA2dpstate(false);
                    EventBus.getDefault().post(new EventCenter<>(Constants.A2DP_ISCONNTECT, false));
                        break;
            }
        }
    }
}
